﻿using System;
using PSE.Deployment.Engine;
using PSE.Deployment.Engine.Enums;
using PSE.Deployment.Engine.Interfaces;

namespace Sinacor.Deployment.Setup.Wizard.Steps
{
    public class DataBaseConnection: BaseStep
    {
        private UI.DataBaseConnection dataBaseConnection;
        public StepNavigationAction NavigationAction { get; protected set; }

        public DataBaseConnection(IStepGroup parent)
            :base(parent)
        {
            this.Id = "DataBaseConnection";

            this.ErrorBehavior = PSE.Deployment.Engine.Enums.StepErrorBehavior.NotifyParent;
            this.NavigationBehavior = PSE.Deployment.Engine.Enums.StepNavigationBehavior.NextPreviousCancel;

            this.ExecutionFinished += new EventHandler<PSE.Deployment.Engine.CustomEventArgs.ExecutionFinishedEventArgs>(DataBaseConnection_ExecutionFinished);
        }

        void DataBaseConnection_ExecutionFinished(object sender, PSE.Deployment.Engine.CustomEventArgs.ExecutionFinishedEventArgs e)
        {
            e.NavigationAction = this.NavigationAction;
        }

        public override void Execute()
        {
            dataBaseConnection = new UI.DataBaseConnection();
            dataBaseConnection.ShowDialog();
            this.NavigationAction = dataBaseConnection.NavigationAction.GetValueOrDefault();

        }

        public override void Undo()
        {
            if (dataBaseConnection != null)
                dataBaseConnection.Close(false);
        }
    }
}
